#pragma once
#include "stdafx.h"
#include "common.h"
#include "scene.h"
#include "ui_sensor_editor.h"
#include "units.h"
#include "parameters_model.h"
#include "indicator.h"
#include "automatic_item.h"

class Common;
class SceneView;
class QGraphicsSceneMouseEvent;
class hydraulicItem;
class ParametersModel;
class Delegate;
class Indicator;
class Sensor;
class autConPoint;
class automaticItem;

class SensorEditor : public QDialog
{
	Q_OBJECT
private:
	Ui::SensorEditorDialog ui;
	Common* common;
	SceneView* scene;
	void setModel(ParametersModel* _model);
	vector<automaticItem*> list_of_item;
	QStringList parameters;
	double pipe_length;
	hydraulicItem* pipe;
public:
	SensorEditor(Common* _common, SceneView* _scene);	
	void setData(vector<automaticItem*> _list_of_item, hydraulicItem* _pipe/*, double _pipe_length = -1*/);
	void setSensor(automaticItem *_sensor);
	vector<automaticItem*> getData();
	/*double getPipeLength(){ return pipe_length; };*/
	int getIndex();
private slots:
	void on_bAdd_clicked();
	void on_bRemove_clicked();
	void displayParameters(int index);
	void displayModel(int index);
	void parameterStateChanged(QListWidgetItem *item);
	void updateSensorName(const QString &name);
	void itemClicked(QListWidgetItem *item);
};

//============================================================================================================

class Sensor : public QObject
{
	Q_OBJECT
public:
	Sensor(Common* _common, SceneView* _scene, automaticItem* _parent, const bool display_title);
	Sensor::~Sensor();
	void init_model();
	ParametersModel* init_parameter_model(const QString &_parameter);
	QString getName();
	
	void setVisibleOnScene(const bool &_visible);
	ParametersModel* getParameterModel(QString _name);
	bool getState(QString _name);
	vector<pair<QString, bool>> getListOfParameter();
	void setParameterState(const QString &_name, const bool &_state);
	//void setParentName(const QString &_parent_name){ parent_name = _parent_name; }
	//QString getParentName(){ return parent_name; }
	QStringList getEnabledParametersName();
	Indicator *getIndicator();
	const hash_map<string, pair<ParametersModel*, bool> > & getListOfParameters();
	ParametersModel* addParameter(const QString &_name, const bool &_calculated);
	double getParameterError(const string &_parameter, const QString &_script, const double &_time);
	QStringList getCalculatedParameters();
	bool isCalculatedParameters(const string &_name);
	double getCoordinate();
	int getDigits(const string &_name); 
	QString getUnit(const string &_name); 
	int countCalcParameters();
	double getWidth();
	automaticItem * getParent(){ return parent; }
	void popupShow(QGraphicsSceneContextMenuEvent *event);
	bool isInLeftPartOfPipe();
private:
	Common* common;
	SceneView* scene;
	automaticItem* parent;
	//QString parent_name;
	hash_map<string, pair<ParametersModel*, bool> > list_of_parameters;
private slots:
	//void update_parameters();
	//void getNewParameterValue(const QString &_name, const QVariant &_value);
	void showSensorEditor();
	void removeScriptParameter(const QString &script_name);
	void addScriptParameter(const QString &script_name);
	void addLayer(const double &time, const double & dt );
protected:
	void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
	void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
signals:
	//void updateName(const QString &_name);
	//void updatePosition(Sensor* _sensor);
	//void setModelData(const int &parent_row, const int &row, const QVariant &value);
	//void setNewParameterValue(const QString &_name, const QVariant &_value);
};
